package com.woniuxy.service.impl;


import com.woniuxy.dao.QuestionDao;
import com.woniuxy.entity.*;
import com.woniuxy.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;

import java.util.List;

/**
 * @author wenlei
 * @ClassName ChoiceQuestionImpl
 * @Description 题目业务实现类
 * @date 2022/4/26 17:27
 **/

@Service
public class QuestionServiceImpl implements QuestionService {

    @Autowired(required = false)
    QuestionDao questionDao;
    /**
     *@author wen
     *@Discription 增加一个单选题
     *@date 2022/4/26 17:37
     **/
    @Override
    @Transactional//开启事务
    public ResponseEntity addSingleChoiceOne(Question question) {
        if(question.getQuTypeId() == 1){
            int count=questionDao.insertChoiceQuestionOne(question);
            int id = question.getId();
            List<ChoiceAnswer> list = question.getChoiceAnswerLists();
            //设置quesId属性
            for (ChoiceAnswer l:list) {
                l.setQuesId(id);
            }
            int count1=questionDao.insertChoiceAnswer(list);
            if(count > 0 && count1 > 0){
                return ResponseEntity.SUCCESSFUL;
            }else {
                return ResponseEntity.FAIL;
            }
        }else {
            return ResponseEntity.FAIL;
        }

    }

    /**
     *@author wen
     *@Discription 增加一个多选题
     *@date 2022/4/26 17:37
     **/
    @Override
    @Transactional//开启事务
    public ResponseEntity addMultipleChoiceOne(Question question) {
        if(question.getQuTypeId() == 1 || question.getQuTypeId() == 2){
            int count=questionDao.insertChoiceQuestionOne(question);
            int id = question.getId();
            List<ChoiceAnswer> list = question.getChoiceAnswerLists();
            for (ChoiceAnswer l:list) {
                l.setQuesId(id);
            }
            int count1=questionDao.insertChoiceAnswer(list);
            if(count > 0 && count1 > 0){
                return ResponseEntity.SUCCESSFUL;
            }else {
                return ResponseEntity.FAIL;
            }
        }else {
            return ResponseEntity.FAIL;
        }
    }

    /**
     *@author wen
     *@Discription 增加一个判断题
     *@date 2022/4/26 18:20
     **/
    @Override
    public ResponseEntity addJudegOne(Question question) {

        int count=questionDao.insertQuestionOne(question);
        if(count > 0){
            return ResponseEntity.SUCCESSFUL;
        }else {
            return ResponseEntity.FAIL;
        }
    }

    /**
     *@author wen
     *@Discription 增加一个问答题
     *@date 2022/4/26 18:40
     **/
    @Override
    public ResponseEntity addEssayQuestionOne(Question question) {
        int count=questionDao.insertQuestionOne(question);
        if(count > 0){
            return ResponseEntity.SUCCESSFUL;
        }else {
            return ResponseEntity.FAIL;
        }
    }

    /**
     *@author wen
     *@Discription 增加一个填空题
     *@date 2022/4/26 18:53
     **/
    @Override
    public ResponseEntity addGapFillingOne(Question question) {
        int count=questionDao.insertQuestionOne(question);
        if(count > 0){
            return ResponseEntity.SUCCESSFUL;
        }else {
            return ResponseEntity.FAIL;
        }
    }

    /**
     *@author wen
     *@Discription 修改题目
     *@date 2022/4/27 11:53
     **/
    @Override
    @Transactional//开启事务
    public ResponseEntity updateQuestion(Question question) {

        if(question.getQuTypeId() == 1 || question.getQuTypeId() == 2){
            //修改选择题
            int count=questionDao.updateQuestionOne(question);
            int id = question.getId();
            List<ChoiceAnswer> list = question.getChoiceAnswerLists();

            for (ChoiceAnswer l:list) {
                l.setQuesId(id);
            }
            list.forEach(System.out::println);
            int count1=questionDao.updateChoiceAnswer(list);
            if(count > 0 && count1 > 0){
                return ResponseEntity.SUCCESSFUL;
            }else {
                return ResponseEntity.FAIL;
            }
        }else {
            //修改非选择题
            int count=questionDao.updateQuestionOne(question);
            if(count > 0){
                return ResponseEntity.SUCCESSFUL;
            }else {
                return ResponseEntity.FAIL;
            }

        }

    }

    /**
     *@author wen
     *@Discription 增加题目
     *@date 2022/4/28 20:03
     **/
    @Override
    @Transactional//开启事务
    public ResponseEntity addQuestionOne(Question question) {
        if(question.getQuTypeId() == 1 || question.getQuTypeId() == 2){
            //增加选择题
            List<ChoiceAnswer> list = question.getChoiceAnswerLists();
            System.out.println(list);
            //为选择题是选项内容list为空 直接执行失败
            if(list.size() == 0){
                return ResponseEntity.FAIL;
            }else{
                int count=questionDao.insertChoiceQuestionOne(question);
                int id = question.getId();
                for (ChoiceAnswer l:list) {
                    l.setQuesId(id);
                }
                int count1=questionDao.insertChoiceAnswer(list);
                if(count > 0 && count1 > 0){
                    return new ResponseEntity(200, "新增成功", null);
                }else {
                    return ResponseEntity.FAIL;
                }
            }
        }else{
            //增加非选择题
            int count=questionDao.insertQuestionOne(question);
            if(count > 0){
                return new ResponseEntity(200, "新增成功", null);
            }else {
                return ResponseEntity.FAIL;
            }
        }

    }
    /**
     *@author
     *@Discription 查询选择题答案
     *@date 2022/4/28 20:03
     **/
    @Override
    public ResponseEntity findSelectAnswer(int id) {
        List<QuestionAnswer> list=questionDao.findSelectAnswerById(id);
        if (list != null) {
            return new ResponseEntity(200,"查询成功",list);
        }
        else {
            return ResponseEntity.FAIL;
        }

    }
}
